﻿using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace CSharpHelpers.Regexs
{
    public class RegexHelper
    {
        //验证大陆，香港，澳门手机号码
        //bool isPhoneNumber = Regex.IsMatch(this.txtMobileNum.Text.Trim(),
        //   @"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$|^((((0?)|((00)?))(((\s){0,2})|([-_－—\s]?)))|(([(]?)[+]?))(853){1}([)]?)([-_－—\s]?)(28[0-9]{2}|((6|8)[0-9]{3}))[-_－—\s]?[0-9]{4}$|((((0?)|((00)?))(((\s){0,2})|([-_－—\s]?)))|(([(]?)[+]?))(852){1}([)]?)([-_－—\s]?)((2|3|5|6|9|8)?([-_－—\s]?)\d{3})(([-_－—\s]?)\d{4})$");

        /// <summary>
        /// 检测字符串是否只包含英文字母、#、空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckStrValid(string str)
        {
            bool isOnlyContainLetter = false;
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^[A-Za-z#]+$");
            if (regex.IsMatch(str))
            {
                isOnlyContainLetter = true;
            }

            return isOnlyContainLetter;
        }

        /// <summary>
        /// 检测字符串是否只包含英文字母、#、空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckStringValid(string str)
        {
            bool isOnlyContainLetter = false;
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^[A-Za-z#\s]+$");
            if (regex.IsMatch(str))
            {
                isOnlyContainLetter = true;
            }

            return isOnlyContainLetter;
        }

        ///// <summary>
        ///// 驗證IP 格式
        ///// </summary>
        ///// <param name="str">被驗證的字符串</param>
        ///// <returns>true: 格式正確</returns>
        //public static bool IsIpAddress(string str)
        //{
        //    if (str.Length <= 0)
        //    {
        //        return false;
        //    }

        //    //else if (str.Trim().ToLower() == "localhost") return true;
        //    return Regex.IsMatch(str,
        //                         @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
        //}

        /// <summary>
        /// 是否为整型字符串
        /// </summary>
        /// <param name="strSource">时间字符串(15:00:00)</param>
        /// <returns></returns>
        public static bool IsInt(string strSource)
        {
            //var isInt = Regex.IsMatch(strSource, @"^[0-9].\d*$");// 匹配如1-1，1.1，。。。

            var isInt = System.Text.RegularExpressions.Regex.IsMatch(strSource, @"^[0-9]+$");// 匹配如1，2，。。。
            //var isInt = Regex.IsMatch(strSource, @"^\d+$");

            return isInt;
        }

        /// <summary>
        /// 正则表达式验证只能输入数字或字母
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns> 
        public static bool IsLetterAndNumber(string strSource)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(strSource, @"^[A-Za-z0-9]+$");
        }

        /// <summary>
        /// 正则表达式验证只能输入字母
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns> 
        public static bool IsLetter(string strSource)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(strSource, @"^[A-Za-z]+$");
        }

        public static bool IsContainCharacter(string str)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str, "[a-zA-Z]");
        }

        /// <summary>
        /// 驗證IP 格式
        /// </summary>
        /// <param name="str">被驗證的字符串</param>
        /// <returns>true: 格式正確</returns>
        public static bool IsIpAddress(string str)
        {
            if (str.Length <= 0)
            {
                return false;
            }

            var isMatch = System.Text.RegularExpressions.Regex.IsMatch(str, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
            return isMatch;
        }

        public const string IsIPTips = "The IP format is incorrect, please input the format as follows: 127.0.0.1.";
        public const string IsTimeTips = " format is incorrect, please input the format as follows: 00:00:01";
        public const string IsIntTips = " format is incorrect, please input the format as follows: 0";

        /// <summary>
        /// 是否为正确IP格式
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static bool IsMatchIP(string strSource)
        {
            if (strSource.Length <= 0)
            {
                return false;
            }

            return System.Text.RegularExpressions.Regex.IsMatch(strSource, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
        }

        /// <summary>
        /// 是否为时间型字符串
        /// </summary>
        /// <param name="strSource">时间字符串(15:00:00)</param>
        /// <returns></returns>
        public static bool IsTime(string strSource)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(strSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }

        ///// <summary>
        ///// 是否为整型字符串
        ///// </summary>
        ///// <param name="strSource">时间字符串(15:00:00)</param>
        ///// <returns></returns>
        //public static bool IsInt(string strSource)
        //{
        //    return Regex.IsMatch(strSource, @"^[0-9]\d*$");
        //}

        /// <summary>
        /// 是否为double型字符串
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="dValue"></param>
        /// <returns></returns>
        public static bool IsDouble(string strSource, out double dValue)
        {
            Double.TryParse(strSource, out dValue);
            return System.Text.RegularExpressions.Regex.IsMatch(strSource, @"^[-+]?[0-9]+(\.[0-9]+)?$");
        }

        public static bool IsDoubleLimitLen(string strSource, int len)
        {
            if (strSource.Contains("."))
            {
                string[] str = strSource.Split('.');

                return str[1].Length <= len;
            }
            return true;
        }

        ///// <summary>
        ///// 正则表达式验证只能输入数字或字母
        ///// </summary>
        ///// <param name="strSource"></param>
        ///// <returns></returns> 
        //public static bool IsLetterAndNumber(string strSource)
        //{
        //    return Regex.IsMatch(strSource, @"^[A-Za-z0-9]+$");
        //}

        /// <summary>
        /// 正确返回 true
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsEmail(string email)
        {
            const string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            return System.Text.RegularExpressions.Regex.IsMatch(email, strRegex);
        }

        /// <summary>
        /// 判断是否是中文
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static bool IsChineseLetter(string strSource)
        {
            return strSource.Any(t => System.Text.RegularExpressions.Regex.IsMatch(t.ToString(), @"^[\u4e00-\u9fa5]$"));
        }

        /// <summary>
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="pattern">模式字符串</param>        
        public static bool IsMatch(string input, string pattern)
        {
            return IsMatch(input, pattern, RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="pattern">模式字符串</param>
        /// <param name="options">筛选条件</param>
        public static bool IsMatch(string input, string pattern, RegexOptions options)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(input, pattern, options);
        }
    }
}
